package itsm.isperp.module.service.slm;

import itsm.isperp.framework.core.context.ContextHolder;
import itsm.isperp.framework.data.dialect.DialectFactory;
import itsm.isperp.framework.domain.IUser;
import itsm.isperp.framework.message.domain.IMessagePacket;
import itsm.isperp.framework.service.BaseService;
import itsm.isperp.framework.web.response.ResultMessage;
import itsm.isperp.module.entity.db.DbEntity;
import itsm.isperp.module.entity.slm.SlmAlarmSetting;
import itsm.isperp.module.repository.slm.SlmAlarmMessageHistoryMapper;
import itsm.isperp.module.repository.slm.SlmAlarmSettingMapper;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jdbc.support.DatabaseType;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

@Service
public class SlmAlarmSettingService extends BaseService<SlmAlarmSetting> {

	@Autowired
	protected SlmAlarmSettingMapper slmAlarmSettingMapper;

	@Autowired
	SlmAlarmObjectService slmAlarmObjectService;

	@Autowired
	SlmAlarmMessageHistoryMapper slmAlarmMessageHistoryMapper;

	@Autowired
	protected JdbcTemplate jdbcTemplate;

	@Override
	public SlmAlarmSettingMapper getRepository() {
		return this.slmAlarmSettingMapper;
	}

	@Override
	public ResultMessage delete(String id) {
		Assert.notNull(id, "id不能为空");

		SlmAlarmObjectService slmAlarmObjectService = ContextHolder
				.getSpringBean("slmAlarmObjectService");
		slmAlarmObjectService.deleteBySettingId(id);

		this.getRepository().deleteByClass(id, SlmAlarmSetting.class);
		return ResultMessage.success();
	}

	public void deleteByServiceTargetId(String serviceTargetId) {

		slmAlarmObjectService.deleteByServiceTargetId(serviceTargetId);
		this.getRepository().deleteByServiceTargetId(serviceTargetId);
	}

	public void deleteByAgreementId(String agreementId) {
		slmAlarmObjectService.deleteByAgreementId(agreementId);

		if (DialectFactory.getDatabaseType() == DatabaseType.MYSQL) {
			this.getRepository().deleteByAgreementId(agreementId);
		} else {
			String sql = "DELETE  slm_alarm_setting WHERE id IN (SELECT a.id FROM slm_alarm_setting a "
					+ "LEFT JOIN slm_service_target b ON a.service_target_id=b.id WHERE b.agreement_id='"
					+ agreementId + "')";

			jdbcTemplate.execute(sql);
		}
	}

	public void deleteByProjectId(String projectId) {
		slmAlarmObjectService.deleteByProjectId(projectId);
		if (DialectFactory.getDatabaseType() == DatabaseType.MYSQL) {

			this.getRepository().deleteByProjectId(projectId);
		} else {
			String sql = "DELETE FROM slm_alarm_setting WHERE id IN (SELECT a.id FROM slm_alarm_setting a "
					+ " LEFT JOIN slm_service_target b ON a.service_target_id=b.id"
					+ " LEFT JOIN slm_service_agreement c ON b.agreement_id=c.id WHERE c.project_id='"
					+ projectId + "')";

			jdbcTemplate.execute(sql);
		}
	}

	// 获取没有被发送到提醒
	public void getNoSendedAlarm() {
		// 包含提醒设置以及slm业务对象的一个列表

		List<Map<String, Object>> list = slmAlarmMessageHistoryMapper
				.findAllDetailBySended(false);

		String slm_busobject_id = "";
		String businessKey = "";
		String messageType = "";
		String title = "";
		String content = "";

		// 解析以获得需要发送到对象
		for (Map<String, Object> m : list) {
			// 对于不同的消息类型需要获取不同的地址（邮件/短信/站内信）

			// 取得带发送邮件，然后解析邮件内容

			// 消息队列
			List<IMessagePacket> ms = new ArrayList<>();
			Map<String, IUser> mapUsers = null;

			// MessageQueue.generalAndAllToMeessageList(businessKey,
			// bNode.getMessageType(), mapUsers, title, content, ms);

		}

	}

	public DbEntity findDbEntityById(String id) {
		return slmAlarmSettingMapper.findDbEntityById(id);
	}

}
